package com.ruicar.afs.cloud.manage.reprint.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruicar.afs.cloud.common.core.util.IResponse;
import com.ruicar.afs.cloud.common.util.EmptyUtils;
import com.ruicar.afs.cloud.manage.common.entity.ManageReprintRecord;
import com.ruicar.afs.cloud.manage.common.service.ManageReprintRecordService;
import com.ruicar.afs.cloud.manage.reprint.condition.ContractReprintCondition;
import io.micrometer.core.instrument.util.StringUtils;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author chuke
 * @create 2020/7/4 14:16
 */
@Slf4j
@RestController
@RequestMapping("/reprint")
@AllArgsConstructor
public class ReprintRecordController {
    private ManageReprintRecordService reprintRecordService;

    @ApiOperation(value = "合同重出的案件列表")
    @PostMapping("/getReprintRecordChangeList")
    public IResponse getManageReprintRecordChangeContractNo (@RequestBody ContractReprintCondition condition){
      return new IResponse().setData(reprintRecordService.page(new Page(condition.getPageNumber(),condition.getPageSize())
            , Wrappers .<ManageReprintRecord>query().lambda()
                      .like(StringUtils.isNotEmpty(condition.getApplyNo()),ManageReprintRecord::getApplyNo,condition.getApplyNo())
                      .like(StringUtils.isNotEmpty(condition.getCustName()),ManageReprintRecord::getCustName,condition.getCustName())
                      .ge(EmptyUtils.isNotEmpty(condition.getCreatTime()),ManageReprintRecord::getEndDate,condition.getCreatTime())
                      .le(EmptyUtils.isNotEmpty(condition.getEndDate()),ManageReprintRecord::getEndDate,condition.getEndDate())));
    }


    @ApiOperation(value = "合同重出信息变更")
    @PostMapping("/changeReprintInfo")
    public IResponse contractBankCardInfoChange (@RequestBody ManageReprintRecord reprintRecord){
        LambdaQueryWrapper<ManageReprintRecord> wrapps = Wrappers.<ManageReprintRecord>lambdaQuery()
                .eq(ManageReprintRecord::getContractNo, reprintRecord.getContractNo());
        ManageReprintRecord print = reprintRecordService.getOne(wrapps);
        //查询到记录就更新，没有就新增
        if (print == null){
            reprintRecordService.save(reprintRecord);
        }else {
            reprintRecordService.update(reprintRecord,wrapps);
        }
        //返回给页面数据
        print = reprintRecordService.getOne(wrapps);
        return new IResponse().setData(print);
    }
}
